package com.cloud.code.牛客网;

import java.util.*;
import java.util.stream.Collectors;

public class Test57 {

    static class Stu implements Comparable {

        Stu(Integer id, List<Integer> list) {
            this.id = id;
            this.list = list;
        }

        Integer id;
        List<Integer> list = new ArrayList<>();
        Integer sum;

        void addCode(Integer num) {
            list.add(num);
            if (list.size() >= 3) {
                Collections.sort(this.list, new Comparator<Integer>() {
                    @Override
                    public int compare(Integer o1, Integer o2) {
                        return o2 - o1;
                    }
                });
                this.sum = list.get(0) + list.get(1) + list.get(2);
            }
        }

        @Override
        public int compareTo(Object o) {
            Stu stu = (Stu) o;
            if (this.sum.equals(stu.sum)) {
                return stu.id - this.id;
            } else {
                return stu.sum - this.sum;
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = Integer.parseInt(scanner.nextLine());
            String ids = scanner.nextLine();
            String code = scanner.nextLine();
            Map<Integer, Stu> map = new TreeMap<>();
            String[] splitids = ids.split(",");
            String[] splitcodes = code.split(",");
            for (int i = 0; i < splitids.length; i++) {
                Integer tem = Integer.valueOf(splitids[i]);
                if (ids.length() - ids.replace(String.valueOf(tem), "").length() < 3) {
                    continue;
                } else {
                    Stu stu = map.get(tem);
                    if (stu == null) {
                        List<Integer> list = new ArrayList<>();
                        list.add(Integer.valueOf(splitcodes[i]));
                        map.put(tem, new Stu(tem, list));
                    } else {
                        stu.addCode(Integer.valueOf(splitcodes[i]));
                    }
                }
            }

            List<Stu> collect = map.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());

            Collections.sort(collect);

            collect.forEach(temp -> {
                System.out.println(temp.id);
            });

        }

    }
}
